package marionito.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import marionito.domaine.Joueur;
import marionito.domaine.JoueurPartie;
import marionito.domaine.Partie;
import marionito.usecases.GestionParties;

/**
 * Servlet implementation class ListeReprendre
 */
@WebServlet(name = "listeReprendre.html")
public class ListeReprendre extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@EJB
	private GestionParties gestionParties;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		List<Partie> listeParties = gestionParties.listerPartiesCommencee();
		List<Partie> liste = new ArrayList<Partie>();
		Joueur joueur = (Joueur) request.getSession().getAttribute("joueur");
		for (Partie p : listeParties){
			if(gestionParties.contientJoueur(p.getNom(), joueur)){
				liste.add(p);
			}
		}
		
		getServletContext().setAttribute("lesParties", liste);
		getServletContext().getNamedDispatcher("reprendrePartie.html").forward(
				request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
